<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>找不同游戏</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style type="text/tailwindcss">
        @layer utilities {
            .difference-highlight {
                @apply absolute opacity-0 rounded-full transition-all duration-300 pointer-events-none;
            }
            .difference-found {
                @apply opacity-60 pointer-events-none;
            }
            .difference-area {
                @apply absolute bg-transparent border-2 border-blue-300 opacity-0 hover:opacity-60 transition-opacity duration-200 cursor-pointer;
            }
            .level-btn {
                @apply px-4 py-2 rounded-lg transition-all duration-300 border-2 focus:outline-none focus:ring-2;
            }
            .level-btn-active {
                @apply bg-blue-500 text-white border-blue-500;
            }
            .level-btn-inactive {
                @apply bg-white text-gray-700 border-gray-300 hover:bg-gray-50;
            }
            .image-container {
                @apply relative max-w-full w-[1000px] h-[750px] bg-gray-100 rounded-lg overflow-hidden shadow-md;
            }
            .mobile-container {
                @apply w-full h-auto max-w-[100vw] overflow-x-auto;
            }
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4 font-sans">
    <div class="w-full max-w-6xl bg-white rounded-xl shadow-xl overflow-hidden">
        <!-- 游戏标题 -->
        <header class="bg-gradient-to-r from-blue-500 to-indigo-600 text-white p-6 text-center">
            <h1 class="text-[clamp(1.8rem,4vw,2.5rem)] font-bold tracking-tight">找不同游戏</h1>
            <p class="mt-2 text-lg opacity-90">在两张图片中找出所有不同之处</p>
        </header>

        <!-- 关卡选择 -->
        <div class="p-4 bg-gray-50 border-b flex flex-wrap justify-center gap-2">
            <h2 class="text-lg font-medium mr-4">选择关卡:</h2>
            <button id="level-1" class="level-btn level-btn-active">关卡 1</button>
            <button id="level-2" class="level-btn level-btn-inactive">关卡 2</button>
            <button id="level-3" class="level-btn level-btn-inactive">关卡 3</button>
        </div>

        <!-- 游戏状态栏 -->
        <div class="flex justify-between items-center p-4 bg-gray-50 border-b">
            <div class="flex items-center">
                <i class="fa fa-clock-o text-amber-500 text-xl mr-2"></i>
                <span class="text-lg font-medium">时间: <span id="timer" class="text-amber-600 font-bold">60</span> 秒</span>
            </div>
            <div class="flex items-center">
                <i class="fa fa-check-circle text-green-500 text-xl mr-2"></i>
                <span class="text-lg font-medium">已找到: <span id="found-count" class="text-green-600 font-bold">0</span>/<span id="total-differences">5</span></span>
            </div>
            <button id="restart-btn" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition-all duration-300 flex items-center">
                <i class="fa fa-refresh mr-2"></i>重新开始
            </button>
        </div>

        <!-- 游戏主体 -->
        <main class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- 原图 -->
                <div class="group mobile-container">
                    <h2 class="text-xl font-semibold mb-2 text-gray-700">原图</h2>
                    <div class="image-container">
                        <img id="original-image" src="https://picsum.photos/seed/forest1a/800/600" alt="原图" class="w-full h-full object-contain">
                        <!-- 差异点标记将通过JS动态添加 -->
                    </div>
                </div>
                
                <!-- 差异图 -->
                <div class="group mobile-container">
                    <h2 class="text-xl font-semibold mb-2 text-gray-700">差异图</h2>
                    <div class="image-container" id="difference-image-container">
                        <img id="difference-image" src="https://picsum.photos/seed/forest1b/800/600" alt="差异图" class="w-full h-full object-contain">
                        <!-- 差异点区域将通过JS动态添加 -->
                    </div>
                </div>
            </div>
        </main>

        <!-- 游戏结果弹窗 -->
        <div id="result-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden transition-opacity duration-300">
            <div class="bg-white rounded-xl shadow-2xl p-8 max-w-md w-full transform transition-all duration-500 scale-95 opacity-0" id="modal-content">
                <div class="text-center">
                    <div id="result-icon" class="mx-auto w-16 h-16 flex items-center justify-center rounded-full mb-4"></div>
                    <h2 id="result-title" class="text-2xl font-bold mb-2"></h2>
                    <p id="result-message" class="text-gray-600 mb-6"></p>
                    <div class="flex justify-center gap-4">
                        <button id="play-again-btn" class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-3 px-8 rounded-lg transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-blue-400">
                            再玩一次
                        </button>
                        <button id="next-level-btn" class="bg-green-500 hover:bg-green-600 text-white font-medium py-3 px-8 rounded-lg transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-green-400">
                            下一关
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 提示信息 -->
        <div class="mt-4 text-center text-gray-600 text-sm">
            <i class="fa fa-lightbulb-o text-yellow-500 mr-1"></i> 提示: 在差异图上点击蓝色框区域寻找不同之处
        </div>
    </div>

    <script>
        // 游戏配置 - 三组关卡（确保每组都有5个差异点）
        const gameLevels = [
            {
                level: 1,
                name: "森林鸟窝",
                totalDifferences: 5,
                timeLimit: 60,
                originalImage: "1.png",
                differenceImage: "2.png",
                differences: [
                    { x: 0.3, y: 0.25, width: 0.1, height: 0.1 },  // 鸟窝中小鸟数量不同
                    { x: 0.65, y: 0.35, width: 0.08, height: 0.08 }, // 灯笼颜色不同
                    { x: 0.45, y: 0.8, width: 0.12, height: 0.1 },  // 花丛中少一朵花
                    { x: 0.75, y: 0.75, width: 0.09, height: 0.09 }, // 石头位置不同
                    { x: 0.45, y: 0.7, width: 0.11, height: 0.07 }  // 树叶形状不同
                ]
            },
            {
                level: 2,
                name: "溪边森林",
                totalDifferences: 5,
                timeLimit: 60,
                originalImage: "3.png",
                differenceImage: "4.png",
                differences: [
                    { x: 0.4, y: 0.2, width: 0.08, height: 0.08 }, // 蜜蜂数量不同
                    { x: 0.6, y: 0.5, width: 0.1, height: 0.1 },   // 藤蔓颜色不同
                    { x: 0.35, y: 0.55, width: 0.12, height: 0.08 }, // 石头形状不同
                    { x: 0.8, y: 0.7, width: 0.09, height: 0.09 },  // 溪水中少一条鱼
                    { x: 0.15, y: 0.75, width: 0.11, height: 0.1 }  // 树上少一片叶子
                ]
            },
            {
                level: 3,
                name: "森林木屋",
                totalDifferences: 5,
                timeLimit: 60,
                originalImage: "5.png",
                differenceImage: "6.png",
                differences: [
                    { x: 0.25, y: 0.35, width: 0.1, height: 0.08 }, // 屋顶颜色不同
                    { x: 0.45, y: 0.3, width: 0.3, height: 0.10 }, // 窗户下花朵颜色不同
                    { x: 0.2, y: 0.7, width: 0.12, height: 0.1 },  // 蘑菇数量不同
                    { x: 0.65, y: 0.65, width: 0.09, height: 0.09 },  // 木门前缺少一个台阶
                    { x: 0.55, y: 0.65, width: 0.08, height: 0.07 }  // 左边树上少一个树洞
                ]
            }
        ];

        // 游戏状态
        const gameState = {
            currentLevel: 1,
            foundDifferences: 0,
            timer: null,
            timeRemaining: 60,
            gameOver: false
        };

        // DOM 元素
        const timerElement = document.getElementById('timer');
        const foundCountElement = document.getElementById('found-count');
        const totalDifferencesElement = document.getElementById('total-differences');
        const originalImageElement = document.getElementById('original-image');
        const differenceImageElement = document.getElementById('difference-image');
        const differenceImageContainer = document.getElementById('difference-image-container');
        const resultModal = document.getElementById('result-modal');
        const modalContent = document.getElementById('modal-content');
        const resultIcon = document.getElementById('result-icon');
        const resultTitle = document.getElementById('result-title');
        const resultMessage = document.getElementById('result-message');
        const restartBtn = document.getElementById('restart-btn');
        const playAgainBtn = document.getElementById('play-again-btn');
        const nextLevelBtn = document.getElementById('next-level-btn');
        
        // 关卡按钮
        const levelButtons = {
            1: document.getElementById('level-1'),
            2: document.getElementById('level-2'),
            3: document.getElementById('level-3')
        };

        // 初始化游戏
        function initGame(level = 1) {
            // 设置当前关卡
            gameState.currentLevel = level;
            const currentLevelData = gameLevels[level - 1];
            
            // 更新UI显示当前关卡
            updateLevelButtons();
            
            // 重置游戏状态
            gameState.foundDifferences = 0;
            gameState.timeRemaining = currentLevelData.timeLimit;
            gameState.gameOver = false;
            
            // 更新UI
            foundCountElement.textContent = gameState.foundDifferences;
            totalDifferencesElement.textContent = currentLevelData.totalDifferences;
            timerElement.textContent = gameState.timeRemaining;
            
            // 更新图片
            originalImageElement.src = currentLevelData.originalImage;
            differenceImageElement.src = currentLevelData.differenceImage;
            
            // 清除之前的差异点标记
            document.querySelectorAll('.difference-highlight, .difference-area').forEach(el => el.remove());
            
            // 添加差异点区域和标记
            setupDifferenceAreas();
            
            // 开始计时
            startTimer();
            
            // 隐藏结果弹窗
            hideResultModal();
        }

        // 更新关卡按钮状态
        function updateLevelButtons() {
            for (let i = 1; i <= 3; i++) {
                if (i === gameState.currentLevel) {
                    levelButtons[i].className = "level-btn level-btn-active";
                } else {
                    levelButtons[i].className = "level-btn level-btn-inactive";
                }
            }
        }

        // 设置差异点区域和标记（基于实际容器尺寸计算）
        function setupDifferenceAreas() {
            const currentLevelData = gameLevels[gameState.currentLevel - 1];
            const container = differenceImageContainer;
            
            // 动态获取容器实际宽高
            const containerWidth = container.offsetWidth;
            const containerHeight = container.offsetHeight;
            
            currentLevelData.differences.forEach((diff, index) => {
                // 将比例坐标转换为像素坐标
                const x = diff.x * containerWidth;
                const y = diff.y * containerHeight;
                const width = diff.width * containerWidth;
                const height = diff.height * containerHeight;
                
                // 创建点击区域（默认半透明可见）
                const diffArea = document.createElement('div');
                diffArea.classList.add('difference-area');
                diffArea.dataset.index = index;
                diffArea.style.left = `${x}px`;
                diffArea.style.top = `${y}px`;
                diffArea.style.width = `${width}px`;
                diffArea.style.height = `${height}px`;
                diffArea.addEventListener('click', handleDifferenceClick);
                container.appendChild(diffArea);
                
                // 创建标记
                createDifferenceMarker(x, y, width, height, index);
                addOriginalImageMarker(x, y, width, height, index);
            });
        }

        // 创建差异点标记（使用像素坐标）
        function createDifferenceMarker(x, y, width, height, index) {
            const marker = document.createElement('div');
            marker.classList.add('difference-highlight', 'bg-green-400');
            marker.id = `difference-marker-${index}`;
            marker.style.left = `${x}px`;
            marker.style.top = `${y}px`;
            marker.style.width = `${width}px`;
            marker.style.height = `${height}px`;
            differenceImageContainer.appendChild(marker);
        }

        // 在原图上添加标记
        function addOriginalImageMarker(x, y, width, height, index) {
            const originalImageContainer = document.querySelector('#original-image').parentNode;
            const marker = document.createElement('div');
            marker.classList.add('difference-highlight', 'bg-green-400', 'pointer-events-none', 'hidden');
            marker.id = `original-marker-${index}`;
            marker.style.left = `${x}px`;
            marker.style.top = `${y}px`;
            marker.style.width = `${width}px`;
            marker.style.height = `${height}px`;
            originalImageContainer.appendChild(marker);
        }

        // 处理差异点点击
        function handleDifferenceClick(e) {
            if (gameState.gameOver) return;
            
            const diffArea = e.currentTarget;
            const index = diffArea.dataset.index;
            
            // 检查是否已经找到
            if (diffArea.classList.contains('difference-found')) return;
            
            // 标记为已找到
            diffArea.classList.add('difference-found');
            diffArea.classList.add('opacity-0'); // 隐藏点击区域
            
            // 显示差异图上的标记
            const differenceMarker = document.getElementById(`difference-marker-${index}`);
            differenceMarker.classList.add('difference-found');
            
            // 显示原图上对应的标记
            const originalMarker = document.getElementById(`original-marker-${index}`);
            originalMarker.classList.remove('hidden');
            
            // 更新已找到数量
            gameState.foundDifferences++;
            foundCountElement.textContent = gameState.foundDifferences;
            
            // 添加找到动画效果
            differenceMarker.classList.add('scale-110');
            setTimeout(() => {
                differenceMarker.classList.remove('scale-110');
            }, 300);
            
            // 检查游戏是否结束
            const currentLevelData = gameLevels[gameState.currentLevel - 1];
            if (gameState.foundDifferences === currentLevelData.totalDifferences) {
                endGame(true);
            }
        }

        // 开始计时器
        function startTimer() {
            if (gameState.timer) clearInterval(gameState.timer);
            
            gameState.timer = setInterval(() => {
                gameState.timeRemaining--;
                timerElement.textContent = gameState.timeRemaining;
                
                // 添加时间快用完的视觉提示
                if (gameState.timeRemaining <= 10) {
                    timerElement.classList.add('text-red-500', 'animate-pulse');
                } else {
                    timerElement.classList.remove('text-red-500', 'animate-pulse');
                }
                
                if (gameState.timeRemaining <= 0) {
                    endGame(false);
                }
            }, 1000);
        }

        // 结束游戏
        function endGame(isWin) {
            gameState.gameOver = true;
            clearInterval(gameState.timer);
            
            // 显示结果弹窗
            showResultModal(isWin);
        }

        // 显示结果弹窗
        function showResultModal(isWin) {
            const currentLevelData = gameLevels[gameState.currentLevel - 1];
            
            if (isWin) {
                resultIcon.className = 'mx-auto w-16 h-16 flex items-center justify-center rounded-full bg-green-100 text-green-500 mb-4';
                resultIcon.innerHTML = '<i class="fa fa-trophy text-3xl"></i>';
                resultTitle.textContent = '恭喜你!';
                
                if (gameState.currentLevel === 3) {
                    resultMessage.textContent = `你完成了所有关卡! 共找到${gameState.foundDifferences}/${currentLevelData.totalDifferences}处不同!`;
                    nextLevelBtn.style.display = 'none';
                } else {
                    resultMessage.textContent = `你通过了关卡 ${gameState.currentLevel}! 共找到${gameState.foundDifferences}/${currentLevelData.totalDifferences}处不同!`;
                    nextLevelBtn.style.display = 'inline-block';
                }
            } else {
                resultIcon.className = 'mx-auto w-16 h-16 flex items-center justify-center rounded-full bg-red-100 text-red-500 mb-4';
                resultIcon.innerHTML = '<i class="fa fa-times text-3xl"></i>';
                resultTitle.textContent = '时间到!';
                resultMessage.textContent = `你找到了${gameState.foundDifferences}/${currentLevelData.totalDifferences}处不同，再试一次吧!`;
                nextLevelBtn.style.display = 'none';
            }
            
            // 显示弹窗
            resultModal.classList.remove('hidden');
            setTimeout(() => {
                modalContent.classList.remove('scale-95', 'opacity-0');
                modalContent.classList.add('scale-100', 'opacity-100');
            }, 10);
        }

        // 隐藏结果弹窗
        function hideResultModal() {
            modalContent.classList.remove('scale-100', 'opacity-100');
            modalContent.classList.add('scale-95', 'opacity-0');
            
            setTimeout(() => {
                resultModal.classList.add('hidden');
            }, 300);
        }

        // 事件监听
        restartBtn.addEventListener('click', () => initGame(gameState.currentLevel));
        playAgainBtn.addEventListener('click', () => initGame(gameState.currentLevel));
        nextLevelBtn.addEventListener('click', () => {
            const nextLevel = Math.min(gameState.currentLevel + 1, 3);
            initGame(nextLevel);
        });
        
        // 关卡选择按钮事件
        for (let i = 1; i <= 3; i++) {
            levelButtons[i].addEventListener('click', () => initGame(i));
        }

        // 初始化游戏
        document.addEventListener('DOMContentLoaded', () => initGame(1));
    </script>
</body>
</html>
